'use strict';

/* Directives */


angular.module('myApp.directives', [])
        .directive('menu', ['$rootScope', 'serviceGame', '$location', 'serviceMenuItem',
            function($rootScope, serviceGame, $location, serviceMenuItem) {
                return {
                    restrict: 'E',
                    scope: {
                    },
                    link: function(scope, element, attrs) {
                        $rootScope.$watch(function() {
                            if (serviceGame.winnerValue) {
                                return 'end';
                            }

                            return serviceGame.nextMoveValue;
                        }, function(newValue, oldValue) {
                            if (newValue) {

                                if (newValue === 'end') {
                                    // gra zakonczona
                                    // info o zwyciezcy
                                    scope.type = newValue;
                                    scope.items = serviceMenuItem.gameEnd;
                                } else {
                                    // gra w toku
                                    // info do kogo nalezy teraz ruch
                                    scope.type = 'start';
                                    if (serviceGame.singleOrMulti === 'single') {
                                        scope.items = serviceMenuItem.gameSingle;
                                    } else {
                                        scope.items = serviceMenuItem.gameMulti;
                                    }
                                }
                            } else {
                                scope.type = null;
                                scope.items = serviceMenuItem.main;
                            }


                        });

                    },
                    templateUrl: 'partials/directives/menu.html',
                    replace: true
                };
            }])
        .directive('footer', ['$rootScope', 'serviceGame', 'serviceStats', 'servicePopupFunc',
            function($rootScope, serviceGame, serviceStats, servicePopupFunc) {
                return {
                    restrict: 'E',
                    scope: {},
                    link: function(scope, element, attrs) {

                        $rootScope.$watch(function() {
                            if (serviceGame.winnerValue) {
                                return 'end';
                            }

                            // unikatowy string
                            if (serviceGame.nextMoveValue) {
                                return serviceGame.timer + '_' + serviceGame.nextMoveValue;
                            }
                        }, function(newValue, oldValue) {
                            if (newValue) {

                                if (newValue === 'end') {
                                    serviceGame.stopTimer();
                                    servicePopupFunc.showPopupGameEnd('Winner is '+serviceGame.getNameForValue(serviceGame.winnerValue));
                                }

                                if (serviceGame.singleOrMulti === 'single') {
                                    if (newValue === 'end') {
                                        // gra zakonczona
                                        // info o zwyciezcy
                                        scope.type = 'single-end';
                                    } else {
                                        // gra w toku
                                        // info do kogo nalezy teraz ruch
                                        scope.type = 'single-start';
                                    }

                                    scope.footerShow = true;

                                } else if (serviceGame.singleOrMulti === 'multi') {
                                    if (newValue === 'end') {
                                        // gra zakonczona
                                        // info o zwyciezcy
                                        scope.type = 'multi-end';
                                    } else {
                                        // gra w toku
                                        // info do kogo nalezy teraz ruch
                                        scope.type = 'multi-start';
                                    }
                                }

                                scope.winnerValue = serviceGame.winnerValue;
                                scope.playerValue = serviceGame.playerValue;
                                scope.nextMoveValue = serviceGame.nextMoveValue;
                                scope.OorX = serviceGame.getNameForValue(serviceGame.winnerValue);
                                scope.nextOorX = serviceGame.getNameForValue(serviceGame.nextMoveValue);
                                scope.movesCount = serviceGame.movesCount;
                                scope.timer = serviceGame.timer;

                                scope.footerShow = true;

                            } else {
                                scope.footerShow = false;
                            }
                        });

                    },
                    templateUrl: 'partials/directives/footer.html',
                    replace: true
                };
            }])
        .directive('popup',
                function() {
                    return {
                        restrict: 'E',
                        scope: {},
                        link: function(scope, element, attrs) {
                            if (typeof attrs.data !== undefined) {
                                var data = angular.fromJson(attrs.data);
                                // do something with data
                                scope.title = data.title || 'Title';
                                scope.info = data.info || 'Information';
                                scope.okBtnId = data.okBtnId || false;
                                scope.cancelBtnId = data.cancelBtnId || false;
                                scope.okBtnText = data.okBtnText || 'Ok';
                                scope.okBtnClass = data.okBtnClass || 'primary';
                                scope.cancelBtnClass = data.cancelBtnClass || 'default';
                                scope.cancelBtnText = data.cancelBtnText || 'Cancel';

                                scope.infoAddId = data.infoAddId || false;
                            }
                        },
                        templateUrl: 'partials/directives/popup.html',
                        replace: true
                    };
                });
